//-----------------Sonic CD Background Script-----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0 : Object.LPlanetYOffset
#alias Object.Value1 : Object.Unused


sub ObjectDraw
	// Draw the solid background color
	TempValue0   = Screen.CenterX
	TempValue0 <<= 1
	DrawRect(0, 0, TempValue0, 100, 0, 0, 96, 255)

	#platform: Use_Decomp
		// Draw a small blue rectangle extension again
		// -> This fixes the gray rectangle that appears with Steam Datafiles
		DrawRect(0, 0, TempValue0, 180, 0, 0, 96, 255)
	#endplatform
	
	// Little Planet Movement
	Sin(TempValue0, Object.LPlanetYOffset)
	TempValue0 >>= 7
	TempValue0  -= 16
	
	DrawSpriteScreenXY(2, 280, TempValue0)
	Object.LPlanetYOffset++
	Object.LPlanetYOffset &= 511

	// Water Movement
	TempValue0 = 3
	TempValue1 = 16
	ArrayPos0  = 40
	while TempValue0 < 39
		Object[ArrayPos0].XPos += TempValue1
		if Object[ArrayPos0].XPos > 0x2000000
			// Wrap the water 512 pixels back
			Object[ArrayPos0].XPos -= 0x2000000
		end if

		TempValue2   = Object[ArrayPos0].XPos
		TempValue2 >>= 16
		ArrayPos0++
		
		DrawSpriteScreenXY(TempValue0, TempValue2, 0)
		TempValue0++
		DrawSpriteScreenXY(TempValue0, TempValue2, 0)
		TempValue0++
		
		TempValue1 += 0x2000
	loop

	// Unknown use, this Value's never referenced anywhere else...
	Object.Unused++
	Object.Unused &= 511
end sub


sub ObjectStartup
	LoadSpriteSheet("Title/Title.gif")
	
	// Note: this three sprite frames are not actually used, the actual mountains are in Logo
	SpriteFrame(0, 0, 176, 68, 256, 4)		// #0 - Left Mountain

	// SpriteFrame(-124, 0, 124, 68, 256, 77) // #1 - Steam/Blit version location of Right Mountain
	
	SpriteFrame(-144, 0, 144, 68, 257, 251)	// #1 - Right Mountain

	SpriteFrame(0, 0, 120, 104, 257, 146)	// #2 - Little Planet
	
	// #4 - #20 rows of water sprites, from back to front
	TempValue0 = 447
	TempValue1 = 168
	while TempValue0 < 511
		SpriteFrame(0, TempValue1, 512, 4, 0, TempValue0)
		SpriteFrame(-512, TempValue1, 512, 4, 0, TempValue0)
		TempValue0 += 4
		TempValue1 += 4
	loop

	// #21 - #22 rows of water sprites, from back to front
	TempValue0 -= 16
	while TempValue1 < 240
		SpriteFrame(0, TempValue1, 512, 4, 0, TempValue0)
		SpriteFrame(-512, TempValue1, 512, 4, 0, TempValue0)
		TempValue0 += 4
		TempValue1 += 4
	loop
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(0, 0, 120, 104, 257, 146)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
